﻿using Microsoft.Xna.Framework;

using block_explorer.NoiseSystem;
using block_explorer.NoiseSystem.Nodes;

namespace block_explorer
{
    class CaveBuilder : IMapBuilder
    {
        INode mNoiseChain;

        public CaveBuilder()
        {
            BuildNoiseChain();
        }

        public Block GetBlock(Vector3 worldPosition)
        {
            double v = mNoiseChain.Value(worldPosition.X, worldPosition.Y, worldPosition.Z);
            return new Block(v > 0.5 ? Block.ContentType.DIRT : Block.ContentType.AIR);
        }

        private void BuildNoiseChain()
        {
            INode baseNoise = new SimplexBasis();
            INode ridgedNoise = new Ridge(baseNoise, 1.0);

            // caves
            INode caves;
            {
                INode result1 = new Fractal(ridgedNoise, 1, 1.0, 1.0, 0.02, 1.0);
                INode result2 = new TranslateDomain(result1, 500.0, 123.4, -245.1);
                INode result = new Combine(result1, result2, Combine.CombineType.Multiply);

                INode blocks = new Select(result, 0.0, 1.0, 0.5, 0.0);

                caves = blocks;
            }

            // mix it all together
            mNoiseChain = caves;
        }
    }
}
